package cn.edu.scnu.semart.goods.service.Impl;

import cn.edu.scnu.semart.goods.service.GoodsService;
import cn.edu.scnu.semart.model.goods.Like;
import cn.edu.scnu.semart.goods.mapper.LikeMapper;
import cn.edu.scnu.semart.goods.service.LikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户点赞Service实现类
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Autowired
    private GoodsService goodsService;

    @Transactional
    @Override
    public boolean likeGoods(Integer userId, Integer goodsId) {
        Like existingLike = baseMapper.selectAnyGoodsLike(userId, goodsId);

        if (existingLike != null) {
            if (existingLike.getDeleted() == null || existingLike.getDeleted() == 0) {
                return false; // 已点赞
            }
            boolean restored = baseMapper.restoreGoodsLike(existingLike.getId()) > 0;
            if (restored) {
                goodsService.incrementLikeCount(goodsId); // 更新点赞数
            }
            return restored;
        }

        Like like = new Like();
        like.setUserId(userId);
        like.setGoodsId(goodsId);
        like.setLikeType(1);
        like.setTargetId(goodsId);

        boolean saved = save(like);
        if (saved) {
            goodsService.incrementLikeCount(goodsId); // 更新点赞数
        }
        return saved;
    }

    @Transactional
    @Override
    public boolean unlikeGoods(Integer userId, Integer goodsId) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("goods_id", goodsId)
                .eq("like_type", 1);
        boolean removed = remove(queryWrapper);
        if (removed) {
            goodsService.decrementLikeCount(goodsId); // 更新点赞数
        }
        return removed;
    }

    @Override
    public boolean likeComment(Integer userId, Integer commentId) {
        // 使用自定义SQL查所有（包括逻辑删除）
        Like existingLike = baseMapper.selectAnyCommentLike(userId, commentId);
        
        if (existingLike != null) {
            // 如果记录存在且未被删除，说明已经点赞
            if (existingLike.getDeleted() == null || existingLike.getDeleted() == 0) {
                return false;
            }
            // 如果记录存在但被逻辑删除，则用手写SQL恢复该记录
            return baseMapper.restoreCommentLike(existingLike.getId()) > 0;
        }
        
        // 如果记录不存在，创建新记录
        Like like = new Like();
        like.setUserId(userId);
        like.setLikeType(2);
        like.setTargetId(commentId);
        
        return save(like);
    }

    @Override
    public boolean unlikeComment(Integer userId, Integer commentId) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .eq("target_id", commentId)
                   .eq("like_type", 2);
        return remove(queryWrapper);
    }

    @Override
    public boolean isLikedGoods(Integer userId, Integer goodsId) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .eq("goods_id", goodsId)
                   .eq("like_type", 1);
        return count(queryWrapper) > 0;
    }

    @Override
    public boolean isLikedComment(Integer userId, Integer commentId) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .eq("target_id", commentId)
                   .eq("like_type", 2);
        return count(queryWrapper) > 0;
    }

    @Override
    public Integer getGoodsLikeCount(Integer goodsId) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId)
                   .eq("like_type", 1);
        return (int) count(queryWrapper);
    }

    @Override
    public Integer getCommentLikeCount(Integer commentId) {
        QueryWrapper<Like> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("target_id", commentId)
                   .eq("like_type", 2);
        return (int) count(queryWrapper);
    }
} 